package com.jadefashion.web;

import com.jadefashion.health.resources.UploadUtils;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Controller
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("upload.do")
    public String upload(@RequestParam("file") MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String fileName = UploadUtils.generateRandonFileName(originalFilename);
        Client client=new Client();
        WebResource resource = client.resource("http://127.0.0.1:80/upload/" + fileName);
        try {
            resource.put(file.getBytes());
            return "redirect:/ok.html";
        } catch (IOException e) {
            e.printStackTrace();
            return "redirect:/ error.html";
        }

    }
}
